iT邦幫忙

2023 iThome 鐵人賽

DAY 13
0

https://ithelp.ithome.com.tw/upload/images/20230928/20130138oOytZj2FVv.jpg
Photo by Fabio Consilvio on Unsplash

上一篇埋個伏筆,今天我們來玩 Swift package 吧。

今天介紹用FLEX加入到 Swift package。
Copy HTTPS 的 URL, 評估一下選多少 Releases version
https://ithelp.ithome.com.tw/upload/images/20230928/20130138W7eJvfhCal.png

https://ithelp.ithome.com.tw/upload/images/20230928/20130138x6x181c9Rh.png

Xcode 導覽頁 -> File -> Add Packages

https://ithelp.ithome.com.tw/upload/images/20230928/20130138blzKdrWLNX.png

https://ithelp.ithome.com.tw/upload/images/20230928/20130138mXsfClil7T.png

設定一下 Up to Next Major Version

Add Packages 後
https://ithelp.ithome.com.tw/upload/images/20230928/20130138gcmsqaE8un.png

FLEX 進入專案囉~

Add new Group, 做好專案資料整理。
建個 Managers 資料夾

https://ithelp.ithome.com.tw/upload/images/20230928/20130138vEupoFkOGh.png

command + N , create a new Swift File

https://ithelp.ithome.com.tw/upload/images/20230928/2013013851mBMy3QU4.png

https://ithelp.ithome.com.tw/upload/images/20230928/20130138268xd6Lc3X.png

import SwiftUI
#if DEBUG
import FLEX
#endif

class LibsManager {
    static let shared = LibsManager()

    func setupLibs() {
        // No need for UIWindow in SwiftUI
        let libsManager = LibsManager.shared
        libsManager.setupFLEX()
    }
    
    func setupFLEX() {
        #if DEBUG
        FLEXManager.shared.isNetworkDebuggingEnabled = true
        #endif
    }
}

extension LibsManager {
    func showFlex() {
        #if DEBUG
        FLEXManager.shared.showExplorer()
        #endif
    }
}

我們用Singleton pattern 來管理套件。

初始化 LibsManager.shared.setupLibs()

init() {
        LibsManager.shared.setupLibs()
    }

放這裡

@main
struct SwimmingStrokesApp: App {
    init() {
        LibsManager.shared.setupLibs()
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

加個神奇的按鈕
加個神奇的按鈕

import SwiftUI

struct FlexButton: View {
    #if DEBUG
    let screenSize = UIScreen.main.bounds.size

    var body: some View {
        Button(action: {
            LibsManager.shared.showFlex()
        }) {
            Text("F")
                .frame(width: 50, height: 50)
                .background(Color.orange)
                .foregroundColor(.white)
                .clipShape(Circle())
                .padding(.all, 10)
        }
        // TODO: 先不要
//        .position(x: screenSize.width - 35, y: screenSize.height - 75)
    }
    #else
    var body: some View {
        EmptyView()
    }
    #endif
}

struct FlexButton_Previews: PreviewProvider {
    static var previews: some View {
        FlexButton()
    }
}

加這段 code

#if DEBUG
FlexButton()
#endif

https://ithelp.ithome.com.tw/upload/images/20230928/20130138TIUeIeqAUF.png

FLEX 很多功能,可以去玩玩看,
我要放連假囉,下集待續


上一篇
Day 12 : Mac M系列遇到 Ruby 坑 (回憶篇
下一篇
Day 14: 我有把 SSH key 放到 GitHub 裡,怎麼沒反應?
系列文
SwiftUI 男孩30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言